package com.daigoudao.web.service;

import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import com.daigoudao.config.ConfigManager;

public class MailSendService {
	
	private static Logger logger = LoggerFactory.getLogger(MailSendService.class);

	public String from;

	private JavaMailSender mailSender;

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public JavaMailSender getMailSender() {
		return mailSender;
	}

	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void sendSimpleEmail(String to, String title, String message)
			throws Exception {
		SimpleMailMessage msg = new SimpleMailMessage();
		msg.setFrom(from);
		msg.setTo(to);
		msg.setSubject(title);
		msg.setText(message);

		try {
			mailSender.send(msg);
		} catch (Exception e) {
			logger.error("sendSimpleEmail error:", e);
			throw e;
		}
	}

	public void sendHtmlEmail(String to, String title, String message)
			throws Exception {
		MimeMessage msg = mailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(msg, true, "UTF-8");
		helper.setTo(to);
		helper.setFrom(from);
		helper.setSubject(title);
		helper.setText(message, true);

		try {
			mailSender.send(msg);
		} catch (Exception e) {
			if(logger.isErrorEnabled()){
				logger.error("sendHtmlEmail error:", e);
			}
			throw e;
		}
	}
	
	public void sendHtmlEmail(String to) throws Exception{
		String title = "欢迎您注册代购岛账户！";
		//String url = ConfigManager.getConfigString("common.rootUrl");
		String message = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><table width=\"700\" border=\"0\" align=\"center\" cellspacing=\"0\" style=\"width:700px;\">"
				+ "<tr><td><div style=\"width:700px;margin:0 auto;border-bottom:1px solid #ccc;margin-bottom:30px;\">"
				+ "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"700\" height=\"39\" style=\"font:12px Tahoma, Arial, 宋体;\"><tr><td width=\"210\">"
				+ "<a target=\"_blank\" href=\"http://www.daigoudao.com\"><img src=\"http://www.daigoudao.com/images/logo.png\" width=\"110\" height=\"39\" border=\"0\"/></a>"
				+ "</td><td width=\"490\" align=\"right\" valign=\"bottom\" style=\"padding-bottom:10px;\">"
				+ "<a target=\"_blank\" style=\"color:#07f;text-decoration:none;font-size:12px;\" href=\"http://www.daigoudao.com/login.htm\">登录</a> | "
				+ "<a target=\"_blank\" style=\"color:#07f;text-decoration:none;padding-right:5px;font-size:12px;\" href=\"http://www.daigoudao.com/help/help.jsp\">帮助中心</a></td>"
				+ "</tr></table></div><div style=\"width:680px;padding:0 10px;margin:0 auto;\"><div style=\"line-height:1.5;font-size:14px;margin-bottom:25px;color:#4d4d4d;\">"
				+ "<strong style=\"display:block;margin-bottom:15px;\">亲爱的会员："
				+ to
				+ "您好！</strong>"
				+ "<p>恭喜您已经成功注册为 代购岛(www.daigoudao.com) 会员，代购岛遍布全球的经纪人竭诚为您提供无忧代购服务！</p>"
				+ "</div></div><div style=\"width:700px;margin:0 auto;\"><div style=\"padding:0 10px;width:285px;margin-bottom:30px;\"><p style=\"padding-top:10px;border-top:1px dashed #ccc;\">"
				+ "<a target=\"_blank\" style=\"color:#07f;text-decoration:none;\" href=\"http://www.daigoudao.com/help/help.jsp\">更多帮助</a>"
				+ "</p></div><div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#999;margin-bottom:20px;line-height:1.3em;font-size:12px;\">"
				+ "<p style=\"margin-bottom:15px;\">此为系统邮件，请勿回复<br/>请保管好您的邮箱，避免代购岛账户被他人盗用</p>"
				+ "<p>如有任何疑问，可查看 <a target=\"_blank\" style=\"color:#666;text-decoration:none;\" href=\"http://www.daigoudao.com/help/help.jsp\">代购岛帮助中心</a>，代购岛网站访问 <a target=\"_blank\" style=\"color:#666;text-decoration:none;\" href=\"http://www.daigoudao.com\">http://www.daigoudao.com</a>"
				+ "<br/>Copyright 代购岛 2011-2012 All Right Reserved</p></div></div></td></tr></table></html>";
		this.sendHtmlEmail(to, title, message);
	}
	
	public void sendHtmlEmail(String to, String v) throws Exception{
		String url = ConfigManager.getConfigString("common.rootUrl");
		url += "/modifypassword.htm?email="+to+"&v="+v;
		
		String title = "找回密码！";
		
		String message = "<html xmlns=\"http://www.w3.org/1999/xhtml\"><table width=\"700\" border=\"0\" align=\"center\" cellspacing=\"0\" style=\"width:700px;\">"
				+ "<tr><td><div style=\"width:700px;margin:0 auto;border-bottom:1px solid #ccc;margin-bottom:30px;\">"
				+ "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"700\" height=\"39\" style=\"font:12px Tahoma, Arial, 宋体;\"><tr><td width=\"210\">"
				+ "<a target=\"_blank\" href=\"http://www.daigoudao.com\"><img src=\"http://www.daigoudao.com/images/logo.png\" width=\"110\" height=\"39\" border=\"0\"/></a>"
				+ "</td><td width=\"490\" align=\"right\" valign=\"bottom\" style=\"padding-bottom:10px;\">"
				+ "<a target=\"_blank\" style=\"color:#07f;text-decoration:none;font-size:12px;\" href=\"http://www.daigoudao.com/login.htm\">登录</a> | "
				+ "<a target=\"_blank\" style=\"color:#07f;text-decoration:none;padding-right:5px;font-size:12px;\" href=\"http://www.daigoudao.com/help/help.jsp\">帮助中心</a></td>"
				+ "</tr></table></div><div style=\"width:680px;padding:0 10px;margin:0 auto;\"><div style=\"line-height:1.5;font-size:14px;margin-bottom:25px;color:#4d4d4d;\">"
				+ "<strong style=\"display:block;margin-bottom:15px;\">亲爱的会员："
				+ to
				+ "您好！</strong>"
				+ "<p> 您在访问 代购岛网站时点击了“找回密码”链接，这是一封密码重置确认邮件。如果您并未尝试修改密码，请忽略本邮件。 </p>"
				+ "<br/><br/> 密码重置：<br/><p> 您可以通过点击以下链接重置帐户密码（基于安全考虑，本链接24小时内有效）。</p>"
				+ "<p><a href=\""+url+"\" target=\"_blank\"><font color=\"red\"><b>点此链接继续</b></font></a></p><p><a href=\""+url+"\" target=\"_blank\"><font color=\"red\">\""+url+"\"</font></a></p>"
				+ "<br/><br/><p>如果您不能点击以上链接，请将该链接复制到浏览器地址栏中访问，也可以完成新密码的创建！</p>"
				+ "</div></div><div style=\"width:700px;margin:0 auto;\"><div style=\"padding:0 10px;width:285px;margin-bottom:30px;\"><p style=\"padding-top:10px;border-top:1px dashed #ccc;\">"
				+ "<a target=\"_blank\" style=\"color:#07f;text-decoration:none;\" href=\"http://www.daigoudao.com/help/help.jsp\">更多帮助</a>"
				+ "</p></div><div style=\"padding:10px 10px 0;border-top:1px solid #ccc;color:#999;margin-bottom:20px;line-height:1.3em;font-size:12px;\">"
				+ "<p style=\"margin-bottom:15px;\">此为系统邮件，请勿回复<br  />请保管好您的邮箱，避免代购岛账户被他人盗用</p>"
				+ "<p>如有任何疑问，可查看 <a target=\"_blank\" style=\"color:#666;text-decoration:none;\" href=\"http://www.daigoudao.com/help/help.jsp\">代购岛帮助中心</a>，代购岛网站访问 <a target=\"_blank\" style=\"color:#666;text-decoration:none;\" href=\"http://www.daigoudao.com\">http://www.daigoudao.com</a>"
				+ "<br/>Copyright 代购岛 2011-2012 All Right Reserved</p></div></div></td></tr></table></html>";
		
		this.sendHtmlEmail(to, title, message);
	}

}
